package xyz.syyrjx.provider.load;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import xyz.syyrjx.provider.SyyrjxRpcProviderClass;
import xyz.syyrjx.provider.conf.SyyrjxRpcProviderConfiguration;
import xyz.syyrjx.provider.task.SyyrjxRpcProviderSchedule;


/**
 * @author 大地崩坏苍蝇兽
 * @date 2023/1/12 - 19:11
 */
public class SyyrjxRpcProviderClientLoadAware implements ApplicationContextAware {

    private final static Logger LOGGER = LoggerFactory.getLogger(SyyrjxRpcProviderClientLoadAware.class);

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SyyrjxRpcProviderEnvironmentSetAware.environmentInit(applicationContext);
        LOGGER.debug("提供者客户端启动自动调用");
        SyyrjxRpcProviderClass providerLoader = applicationContext.getBean(SyyrjxRpcProviderClass.class);
        if (SyyrjxRpcProviderConfiguration.getConfiguration().getRegistryAddress().length > 0) {
            while (true) {
                try {
                    providerLoader.createProviderMap(applicationContext);
                    SyyrjxRpcProviderSchedule schedule = new SyyrjxRpcProviderSchedule();
                    providerLoader.providerClientRun(schedule);
                    break;
                } catch (Exception e) {
                    LOGGER.error("服务器启动失败:" + e.getMessage());
                }
            }
        }
    }
}
